<?php
    class ProductController extends HomeController{
        public function actionDetail($id){
            $sugest_products = Product::model()->findAll(array(
                'condition' => 'status=1 AND feature=1',
                'order' => 'rank DESC',
                'limit' => 3
            ));
            $model = Product::model()->findByPk($id);
            $this->pageTitle = $model->name;
            $criteria = new CDbCriteria();
            $criteria->condition = 'status=1 AND categoryId=' . $model->categoryId . ' AND id>' . $model->id;
            $criteria->limit = 8;
            $criteria->order = 'id DESC';
            $items = Product::model()->findAll($criteria);
            $info = Page::model()->findByUrl('thongtinmuahang');
            $info = isset($info) ? $info->content : '';
            $this->render('detail', array(
                'items' => $items,
                'model' => $model,
                'info' => $info,
                'sugest_products' => $sugest_products,
            ));
        }
        
        public function actionList($id)
        {
            $category = Category::model()->findByPk($id);
            $this->pageTitle = $category->name;
            $criteria = new CDbCriteria();
            $criteria->condition = 'status=1 AND categoryId=' . $category->id;
            $criteria->order = 'rank DESC,id DESC';

            $count = Product::model()->count($criteria);
            $pages = new CPagination($count);
            //results per page
            $pages->pageSize = 12;
            $pages->applyLimit($criteria);
            $items = Product::model()->findAll($criteria);
            
            $this->render('grid', array(
                'items' => $items,
                'pages' => $pages,
                'category' => $category
            ));
        }
        public function actionListGrid($id)
        {
            $category = Category::model()->findByPk($id);
            $this->pageTitle = $category->name;
            $criteria = new CDbCriteria();
            $criteria->condition = 'status=1 AND categoryId=' . $category->id;
            $criteria->order = 'rank DESC,id DESC';

            $count = Product::model()->count($criteria);
            $pages = new CPagination($count);
            //results per page
            $pages->pageSize = 12;
            $pages->applyLimit($criteria);
            $items = Product::model()->findAll($criteria);
            
            $this->render('list', array(
                'items' => $items,
                'pages' => $pages,
                'category' => $category
            ));
        }
        public function actionSearch()
        {
            $key = isset($_REQUEST['v']) ? $_REQUEST['v'] : '';
            $this->pageTitle = 'Tìm sản phẩm: ' . $key;
            if (!empty($key)) {
                $criteria = new CDbCriteria();
                $criteria->condition = 'status=1 AND name LIKE "%' . $key . '%"';
                $criteria->order = 'rank DESC,id DESC';

                $count = Product::model()->count($criteria);
                $pages = new CPagination($count);
                //results per page
                $pages->pageSize = 16;
                $pages->applyLimit($criteria);
                $items = Product::model()->findAll($criteria);
                $number_result = count($items);
                $this->render('search', array(
                    'items' => $items,
                    'pages' => $pages,
                    'number_result' => $number_result,
                ));
            } else {
                $this->pageTitle = 'ERROR';
                throw new CHttpException(400, 'Bạn phải nhập từ khóa tìm kiếm');
            }
        }
        
    }

?>